//
//  UIViewController+LSF.swift
//  hoonpay
//
//  Created by 李胜锋 on 2017/8/8.
//  Copyright © 2017年 lishengfeng. All rights reserved.
//

import UIKit


extension UIViewController{

    
    public static func viewController(storyboardName:String,
                               identifier:String) -> UIViewController {
        let storyboard = UIStoryboard.init(name: storyboardName, bundle: nil)
        return storyboard.instantiateViewController(withIdentifier: identifier)
    }
    
    public static func viewController(storyboardName:String) -> UIViewController {
        
        let type = NSStringFromClass(self.classForCoder())
        let identifier = type.components(separatedBy: ".").last
        return self.viewController(storyboardName: storyboardName, identifier: identifier!)
    }
    
    ///获取当前显示的VC
    public static func lsf_currentVC() -> UIViewController? {
        var result: UIViewController?
        
        var window = UIApplication.shared.keyWindow
        if window?.windowLevel != UIWindowLevelNormal {
            let windows = UIApplication.shared.windows
            for tmpWin in windows {
                if tmpWin.windowLevel == UIWindowLevelNormal {
                    window = tmpWin
                }
            }
        }
        
        let frontView = window?.subviews.first
        let nextResponse = frontView?.next
        if nextResponse is UIViewController {
            result = nextResponse as? UIViewController
        }
        else {
            result = window?.rootViewController
        }
        return result
    }
}
